package net.ib.mtalk.ui.dialog;

import java.util.ArrayList;

import net.ib.mtalk.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<DialogListItem> mItems = new ArrayList<DialogListItem>();
	
	public DialogAdapter(Context context, ArrayList<DialogListItem> items){
		mContext = context;
		mItems = items;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DialogListItem item = (DialogListItem) getItem(position);
		
		View v;
		if(convertView == null){
			v = newView(mContext, parent);
		}else{
			v = convertView;
		}
		
		ViewHolder holder = new ViewHolder();
		holder = (ViewHolder) v.getTag();
		
		holder.dialogItemImg.setImageDrawable(item.image);
		holder.dialogItemText.setText(item.text);
		holder.dialogItemImg2.setImageDrawable(item.image2);
		if(item.image2 != null || item.image == null){
			holder.dialogItemImg.setVisibility(View.GONE);
		}
		
		return v;
	}

	private static class ViewHolder{
		ImageView dialogItemImg;
		TextView dialogItemText;
		ImageView dialogItemImg2;
	}
	
	public View newView(Context context, ViewGroup parent){
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.dialog_menu_item, parent, false);
		
		ViewHolder holder = new ViewHolder();
		holder.dialogItemImg = (ImageView) v.findViewById(R.id.dialog_item_img);
		holder.dialogItemText = (TextView) v.findViewById(R.id.dialog_item_text);
		holder.dialogItemImg2 = (ImageView) v.findViewById(R.id.dialog_item_img2);
		
		v.setTag(holder);
		return v;		
	}	
	
	@Override
	public int getCount() {
		return mItems.size();
	}
	
	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
}